var /* quickNavigation = require("./../../components/quick-navigation/quick-navigation.js"),  */App = getApp(), interval = 0, page_first_init = !0, timer = 1, fullScreen = !1;

Page({
    data: {
        x: getApp().core.getSystemInfoSync().windowWidth,
        y: getApp().core.getSystemInfoSync().windowHeight,
        left: 0,
        show_notice: !1,
        animationData: {},
        play: -1,
        time: 0,
        buy: !1,
        opendate: !1,
        header_idx:0,
        navbar_idx:0,
        show_navbar:true,
        show_share_modal_data:{},
        topNav:[],
        popupShow:false,
        popupData:{},
        doc_idx:0,
        diary_idx:0
    },
    onLoad: function(t) {
        //this.setData(({user_info_show:1}))
        getApp().page.onLoad(this, t);
        this.loadData(t);
        //quickNavigation.init(this);//快捷导航
        this.getCase(this);
        //this.getDiary(this);
        // this.popup(this);
    },
    onShow: function() {
        var e = this;
        getApp().page.onShow(this), getApp().getConfig(function(t) {
            var a = t.store;
            a && a.name && getApp().core.setNavigationBarTitle({
                title: a.name
            }), a && 1 === a.purchase_frame ? e.suspension(e.data.time) : e.setData({
                buy_user: ""
            });
        }), e.notice();
    },
    loadData: function(t) {
        var a = this, e = getApp().core.getStorageSync(getApp().const.PAGE_INDEX_INDEX);
        e && (e.act_modal_list = [], a.setData(e)), getApp().request({
            url: getApp().api.default.index,
            success: function(t) {
                console.log('index',t)
                0 == t.code && (page_first_init ? a.data.user_info_show || (page_first_init = !1) : t.data.act_modal_list = [], 
                a.setData(t.data), getApp().core.setStorageSync(getApp().const.PAGE_INDEX_INDEX, t.data), 
                a.miaoshaTimer());
            },
            complete: function() {
                getApp().core.stopPullDownRefresh();
            }
        });
    },

    /* 购买记录 */
    suspension: function() {
        var s = this;
        interval = setInterval(function() {
            getApp().request({
                url: getApp().api.default.buy_data,
                data: {
                    time: s.data.time
                },
                method: "POST",
                success: function(t) {
                    if (0 == t.code) {
                        var a = !1;
                        s.data.msgHistory == t.md5 && (a = !0);
                        var e = "", i = t.cha_time, o = Math.floor(i / 60 - 60 * Math.floor(i / 3600));
                        e = 0 == o ? i % 60 + "秒" : o + "分" + i % 60 + "秒", !a && t.cha_time <= 300 ? s.setData({
                            buy: {
                                time: e,
                                type: t.data.type,
                                url: t.data.url,
                                user: 5 <= t.data.user.length ? t.data.user.slice(0, 4) + "..." : t.data.user,
                                avatar_url: t.data.avatar_url,
                                address: 8 <= t.data.address.length ? t.data.address.slice(0, 7) + "..." : t.data.address,
                                content: t.data.content
                            },
                            msgHistory: t.md5
                        }) : s.setData({
                            buy: !1
                        });
                    }
                }
            });
        }, 1e4);
    },

    /* 公告 */
    notice: function() {
        var t = this.data.notice;
        if (void 0 !== t) t.length;
    },
    showNotice: function() {
        this.setData({
            show_notice: !0
        });
    },
    closeNotice: function() {
        this.setData({
            show_notice: !1
        });
    },

    /* 秒杀定时器 */
    miaoshaTimer: function() {
        var t = this;
        t.data.miaosha && 0 != t.data.miaosha.rest_time && (t.data.miaosha.ms_next || (timer = setInterval(function() {
            0 < t.data.miaosha.rest_time ? (t.data.miaosha.rest_time = t.data.miaosha.rest_time - 1, 
            t.data.miaosha.times = t.setTimeList(t.data.miaosha.rest_time), t.setData({
                miaosha: t.data.miaosha
            })) : clearInterval(timer);
        }, 1e3)));
    },

    /* 加入购物车 */
    showshop: function(t) {
        var a = this, e = t.currentTarget.dataset.id, i = t.currentTarget.dataset;
        getApp().request({
            url: getApp().api.default.goods,
            data: {
                id: e
            },
            success: function(t) {
                0 == t.code && a.setData({
                    data: i,
                    attr_group_list: t.data.attr_group_list,
                    goods: t.data,
                    showModal: !0
                });
            }
        });
    },

    /* 抽奖 */
    popup(t){
        getApp().getUser().id && getApp().request({
            url:getApp().api.siteinfo.apiroot+'action/guanfangIndexPop',
            data:{
                storeId:getApp().api.siteinfo.acid,
                userId:getApp().getUser().id
            },
            success:function (e) {
                var dd = e.data[0] || {}
                if(e.result != 0 || dd.status == 0) return;
                dd.path = dd.jump_url;
                dd.path_type = dd.jump_type;
                t.setData({
                    popupShow:true,
                    popupData:dd
                })
            }

        })
    },
    popupHide(){
        this.setData({
            popupShow:false
        })
    },
    is_web(e){
      var str='', url = e.target.dataset.item
      if(!url) return;
      if(url.path_type==2){
          str = getApp().querystring(url.path)
      }else {
          str = url.path
      }
      console.log(str);
        this.setData({
            popupShow:false
        },wx.navigateTo({url:str}))
    },

    /* top-nav跳转 */
    headerClick(e){
        console.log(e)
        var str = '',item = e.target.dataset.item;
        if(item.nav_path.indexOf('index/index')>-1) return;
        if(item.jump_type == 2){
            str = getApp().querystring(item.url)
        }else {
            str ='/'+item.nav_path
        }
        console.log(str)
        wx.navigateTo({url:str})
    },
    
    docClick(e){
      var idx = e.target.dataset.idx;
      this.setData({
        doc_idx:idx
      })
  },
  diaryClick(e){
    var idx = e.target.dataset.idx;
    this.setData({
      diary_idx:idx
    })
  },
    
    /* ad */
    loog_ad_1(){
        wx.showToast({
            title:'即将上线',
            image:'../../images/icon-warning.png',
        })
    },

    /* get日记 */
    getDiary(t){
        getApp().request({
            url:getApp().api.siteinfo.apiroot+'diary/index',
            data:{
                storeId:getApp().api.siteinfo.acid,
                userId:wx.getStorageSync('ACCESS_TOKEN')
            },
            success(d){
                console.log('getDiary',d)
                t.setData({
                    diary:d.diarys
                })
            }
        })
    },
    /* 日记跳转 */
    goWebView(e){
        wx.navigateTo({
            url:getApp().querystring(e.currentTarget.dataset.url)
        })
    },
    /* 日记点赞 */
    dianzan(e){
        var t = this, d = e.currentTarget.dataset;
        d.item.is_zan == 2 && getApp().request({
            url:getApp().api.siteinfo.apiroot+'diary/zan',
            data:{
                diaryId:d.item.id,
                userId:wx.getStorageSync('ACCESS_TOKEN')
            },
            success(){
                t.data.diary[d.idx].is_zan = 1;
                t.data.diary[d.idx].zan += 1;
                t.setData({
                    diary:t.data.diary
                })
            }
        })
    },
    /* 分享日记 */
    showShareModal: function(e) {
        var t = this
        getApp().request({
            url:getApp().api.siteinfo.apiroot+'diary/queryDiaryShareInfo',
            data:{
                diaryId:e.currentTarget.dataset.item.id,
                userId:wx.getStorageSync('ACCESS_TOKEN')
            },
            success(m){
                t.setData({
                    share_modal_active: "active",
                    no_scroll: !0,
                    show_share_modal_data:m,
                    show_share_modal_item:e.currentTarget.dataset.item
                });
            }
        })
    },
    shareModalClose: function() {
        this.setData({
            share_modal_active: "",
            no_scroll: !1
        });
    },
    /* 日记商品二维码 */
    getGoodsQrcode: function() {
        var realImg;
        var t = this;
        t.setData({
            goods_qrcode_active: "active",
            share_modal_active: ""
        });
        getApp().request({
            url: getApp().api.siteinfo.apiroot+'action/imgCode',
            data: {
                storeId:getApp().api.siteinfo.acid,
                diaryId:t.data.show_share_modal_item.id,
                userToken: wx.getStorageSync('ACCESS_TOKEN'),
                pageUrl:'pages/index/index'
            },
            success: function(o) {
                console.log("realImg=" + o.imgurl);
                0 == o.result && t.setData({
                    goods_qrcode: o.imgurl
                }), -1 == o.result && (t.goodsQrcodeClose(), getApp().core.showModal({
                    title: "提示",
                    content: o.errmsg,
                    showCancel: !1
                }));
            }
        });
    },
    goodsQrcodeClose: function() {
        this.setData({
            goods_qrcode_active: "",
            no_scroll: !1,
            goods_qrcode:''
        });
    },
    saveGoodsQrcode: function() {
        var e = this;
        getApp().core.saveImageToPhotosAlbum ? (getApp().core.showLoading({
            title: "正在保存图片",
            mask: !1
        }), getApp().core.downloadFile({
            url: e.data.goods_qrcode,
            success: function(o) {
                getApp().core.showLoading({
                    title: "正在保存图片",
                    mask: !1
                }), getApp().core.saveImageToPhotosAlbum({
                    filePath: o.tempFilePath,
                    success: function() {
                        getApp().core.showModal({
                            title: "提示",
                            content: "商品海报保存成功",
                            showCancel: !1
                        });
                    },
                    fail: function(o) {
                        getApp().core.showModal({
                            title: "图片保存失败",
                            content: o.errMsg,
                            showCancel: !1
                        });
                    },
                    complete: function(o) {
                        getApp().core.hideLoading();
                    }
                });
            },
            fail: function(o) {
                getApp().core.showModal({
                    title: "图片下载失败",
                    content: o.errMsg + ";" + e.data.goods_qrcode,
                    showCancel: !1
                });
            },
            complete: function(o) {
                getApp().core.hideLoading();
            }
        })) : getApp().core.showModal({
            title: "提示",
            content: "当前版本过低，无法使用该功能，请升级到最新版本后重试。",
            showCancel: !1
        });
    },
    stopPropagation(){},

    /* get案例 */
    getCase(t){
        App.request({
            url:getApp().api.siteinfo.apiroot+'Index/index',
            data:{
                store_id:App.api.siteinfo.acid
            },
            success:function (e) {
                e.doctor.forEach(function (item, index,arr) {
                  item.data.forEach(function (it, idx) {
                    arr[index].data[idx].long_content = arr[index].data[idx].long_content.replace(/\$/g,'\n')
                  })
                })
                e.diary.forEach(function (item, index,arr) {
                  arr[index].diary_cat_name = arr[index].diary_cat_name.replace('达人秀-','')
                })
                t.setData({
                    topNav:e.topNav,
                    adv:e.adv,
                    doctor:e.doctor,
                    diary:e.diary
                })
            }
        })
    },
    
    onPullDownRefresh: function() {
        getApp().getStoreData(), clearInterval(timer), this.loadData();
        //this.getCase(this)
        //this.getDiary(this)
    },
    onShareAppMessage: function(t) {
        if(this.data.show_share_modal_data.url){
            return {
                path:getApp().querystring(this.data.show_share_modal_item.url,'web'),
                title:this.data.show_share_modal_data.diaryName,
                imageUrl:this.data.show_share_modal_data.picurl
            }
        }else {
            getApp().page.onShareAppMessage(this);
            return {
                path: "/pages/index/index?user_id=" + getApp().getUser().id,
                title: this.data.store.name
            }
        }

    },

    /* 领取优惠券 */
    receive: function(t) {
        var e = this, a = t.currentTarget.dataset.index;
        getApp().core.showLoading({
            title: "领取中",
            mask: !0
        }), e.hideGetCoupon || (e.hideGetCoupon = function(t) {
            var a = t.currentTarget.dataset.url || !1;
            e.setData({
                get_coupon_list: null
            }), wx.navigateTo({
                url: a || "/pages/list/list"
            });
        }), getApp().request({
            url: getApp().api.coupon.receive,
            data: {
                id: a
            },
            success: function(t) {
                getApp().core.hideLoading(), 0 == t.code ? e.setData({
                    get_coupon_list: t.data.list,
                    coupon_list: t.data.coupon_list
                }) : (getApp().core.showToast({
                    title: t.msg,
                    duration: 2e3
                }), e.setData({
                    coupon_list: t.data.coupon_list
                }));
            }
        });
    },
    closeCouponBox: function(t) {
        this.setData({
            get_coupon_list: ""
        });
    },

    /* 拨号 */
    to_dial: function() {
        var t = this.data.store.contact_tel;
        getApp().core.makePhoneCall({
            phoneNumber: t
        });
    },

    onHide: function() {
        getApp().page.onHide(this), this.setData({
            play: -1
        }), clearInterval(interval);
    },
    onUnload: function() {
        getApp().page.onUnload(this), this.setData({
            play: -1
        }), clearInterval(timer), clearInterval(interval);
    },
    
    closeActModal: function() {
        var t, a = this, e = a.data.act_modal_list, i = !0;
        for (var o in e) {
            var s = parseInt(o);
            e[s].show && (e[s].show = !1, void 0 !== e[t = s + 1] && i && (i = !1, setTimeout(function() {
                a.data.act_modal_list[t].show = !0, a.setData({
                    act_modal_list: a.data.act_modal_list
                });
            }, 500)));
        }
        a.setData({
            act_modal_list: e
        });
    },

    /* 视频播放 */
    play: function(t) {
        this.setData({
            play: t.currentTarget.dataset.index
        });
    },
    onPageScroll: function(t) {
        var a = this;
        if (!fullScreen && -1 != a.data.play) {
            var e = getApp().core.getSystemInfoSync().windowHeight;
            "undefined" == typeof my ? getApp().core.createSelectorQuery().select(".video").fields({
                rect: !0
            }, function(t) {
                (t.top <= -200 || t.top >= e - 57) && a.setData({
                    play: -1
                });
            }).exec() : getApp().core.createSelectorQuery().select(".video").boundingClientRect().scrollOffset().exec(function(t) {
                (t[0].top <= -200 || t[0].top >= e - 57) && a.setData({
                    play: -1
                });
            });
        }

        this.data.fix || this.setData({fix:true})
    },
    fullscreenchange: function(t) {
        fullScreen = !!t.detail.fullScreen;
    },

    /* 客服图标 */
    onMyEvent(){
        this.setData({fix:false})
    }
});